package com.nkzy.bigeventback.controller;

import com.nkzy.bigeventback.entity.Result;
import com.nkzy.bigeventback.util.AliOssUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.*;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/upload")
public class FileUploadController  {
    @PostMapping
    public Result uplode(MultipartFile file) throws Exception{
//      System.out.println("zjd250")
        //1.生成一个唯一文件名,防止图片被覆盖
        String uuid = UUID.randomUUID().toString();
        String name = file.getOriginalFilename();
        String kzm = name.substring(name.lastIndexOf("."));//.扩展名
        String fileName=uuid+kzm;
        //2.调用api
        String url = AliOssUtil.uploadFile(fileName, file.getInputStream());
        //file.getInputStream()
        return Result.success(url);
    }
}













